package com.tobby.camel.camelinaction.chapter3.order;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class PurchaseOrderVelocityTest extends CamelTestSupport {

	@Override
	public RouteBuilder createRouteBuilder() {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct:sendMail")
						.setHeader("Subject", constant("Thanks for your ordering"))
						.setHeader("From", constant("donotreply@riders.com"))
						.to("velocity://com/tobby/camel/camelinaction/chapter3/order/mail.vm")
						.to("log:mail").to("mock:mail");
			}
		};
	}

	@Test
	public void testVelocity() throws InterruptedException {
		MockEndpoint endpoint = getMockEndpoint("mock:mail");
		endpoint.expectedMessageCount(1);
		endpoint.message(0).header("Subject").isEqualTo("Thanks for your ordering");
		endpoint.message(0).header("From").isEqualTo("donotreply@riders.com");
		endpoint.message(0).body().contains(
				"Thank you for ordering 1.0 piece(s) of Camel in Action at a cost of 4995.0.");

		PurchaseOrder order = new PurchaseOrder("Camel in Action", 4995, 1);

		template.sendBody("direct:sendMail", order);
		assertMockEndpointsSatisfied();
	}

}
